﻿package core.drawing
{
    import core.enum.*;
    import flash.events.*;
    import flash.geom.*;

    final public class TypeTool extends Tool
    {
        var pt:Point;
        var po:Point;

        public function TypeTool(param1:Tuyitu)
        {
            super(param1, ToolType.Type, true);
            ctrl = ToolType.Move;
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            var _loc_2:Point = null;
            this.UpdateReferences();
            if (type.attitude.state == "add")
            {
                _loc_2 = canvas.GetMousePosition();
                document.AddTextLayer(-1, new Point(_loc_2.x, _loc_2.y - options.size + 5), options.font, options.size, ed.toolbar.GetColor());
                type.attitude.state = "edit";
                ed.layers.Update();
                layer = canvas.layer;
                ed.options.type.panel.x = ed.mouseX - 20;
                ed.options.type.panel.y = ed.mouseY + 10;
                if (ed.options.type.panel.x + 100 > ed.stage.stageWidth)
                {
                    ed.options.type.panel.x = ed.stage.stageWidth - 100;
                }
                if (ed.options.type.panel.y + 100 > ed.stage.stageHeight)
                {
                    ed.options.type.panel.y = ed.stage.stageHeight - 100;
                }
                history.Prepare(Action.LayerAddText, true);
                ed.cursor.Set(type);
            }
            if (type.attitude.state == "edit")
            {
                if (!layer.editMode && ed.options.type)
                {
                    ed.options.type.panel.Show();
                    type.attitude.state = "move";
                    ed.cursor.Set(type);
                }
            }
            else if (type.attitude.state == "move")
            {
                ed.cursor.freeze = true;
                ed.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false, 1, true);
                ed.addEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false, 1, true);
                this.pt = canvas.GetMousePosition();
            }
            return;
        }// end function

        override public function CleanUp() : void
        {
            if (layer != null && ed.options.type)
            {
                if (layer.text != null)
                {
                    layer.name = layer.text.text.substring(0, 100);
                }
                ed.options.type.panel.Hide();
                ed.layers.Update();
            }
            type.attitude.state = "add";
            layer = null;
            return;
        }// end function

        override public function Reset() : void
        {
            if (layer != null)
            {
                this.CleanUp();
            }
            return;
        }// end function

        override public function MouseDrag(event:MouseEvent) : void
        {
            this.po = canvas.GetMousePosition();
            this.pt.x = this.po.x - this.pt.x;
            this.pt.y = this.po.y - this.pt.y;
            document.MoveLayer(this.pt);
            this.pt = this.po;
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            ed.cursor.freeze = false;
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

        override public function MouseMove(event:MouseEvent) : void
        {
            if (ed.canvas.layer.type == LayerType.Text)
            {
                switch(type.attitude.state)
                {
                    case "add":
                    {
                        if (ed.canvas.layer.text.hitTestPoint(ed.stage.mouseX, ed.stage.mouseY))
                        {
                            type.attitude.state = "edit";
                            ed.cursor.Set(type);
                        }
                        break;
                    }
                    case "edit":
                    {
                        if (!ed.canvas.layer.text.hitTestPoint(ed.stage.mouseX, ed.stage.mouseY))
                        {
                            if (ed.canvas.layer.editMode)
                            {
                                type.attitude.state = "add";
                            }
                            else
                            {
                                type.attitude.state = "add";
                            }
                            ed.cursor.Set(type);
                        }
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

    }
}
